package com.ymatou.iapi.m.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.UpdateApplyProductsBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymatou.iapi.m.couponservice.service.UpdateApplyProductsCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponManageCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponProduct;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponProductWapper;
/**
*更新优惠券适用商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateApplyProducts{
	private static UpdateApplyProductsBean updateapplyproductsBean;
	private static UpdateApplyProductsCall updateapplyproductsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新优惠券适用商品");
	}

	@Before
	public void caseUp() {
		updateapplyproductsBean=new UpdateApplyProductsBean();
		updateapplyproductsCall=new UpdateApplyProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateapplyproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateApplyProducts_001() {
		Logger.start(true,"更新优惠券适用商品-平台批次");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			CreateCouponBean createcouponBean = couponManageCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			
			List<String> productIds = new ArrayList<>();
			productIds.add("1233");
			createcouponBean.setProductIds(productIds );
			
			CreateCouponCall createCouponCall = couponManageCallService.createPlatformCoupon(createcouponBean);

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			int batchId =batch.getiBatchId();
			
			updateapplyproductsBean.setBatchId(batchId);
	
			List<String> productCodes = new ArrayList<>();
			
			productCodes.add("Abc1233");
			productCodes.add("bgg224");
			
			updateapplyproductsBean.setProductCodes(productCodes);
			
			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();	
			
			Logger.verifyEquals("操作成功！", updateapplyproductsCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("0", updateapplyproductsCall.getCode(), "验证返回StatusCode");

			List<YmtCouponProduct> products = new YmtCouponProductWapper().selectBysBatchId(batchId);
			
			Logger.verifyEquals(productCodes.size(), products.size(), "验证YmtCouponProduct表生成相应记录数");
			
			for(int i=0;i<products.size();i++)
			{
				YmtCouponProduct product = products.get(i);
				Logger.verifyEquals(productCodes.get(i), product.getProductCode(), "验证YmtCouponProduct表productCodes");

			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateApplyProducts_002() {
		Logger.start(true,"BatchId空");
		try {
			List<String> productCodes = new ArrayList<>();
			
			productCodes.add("Abc1233");
			productCodes.add("bgg224");
			
			updateapplyproductsBean.setProductCodes(productCodes);
			
			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();	
			
			Logger.verifyIsContains("批次不存在", updateapplyproductsCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("2", updateapplyproductsCall.getCode(), "验证返回StatusCode");

			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateApplyProducts_003() {
		Logger.start(true,"BatchId不存在");
		try {
		
			int batchId =4566677;
			
			updateapplyproductsBean.setBatchId(batchId);
	
			List<String> productCodes = new ArrayList<>();
			
			productCodes.add("Abc1233");
			productCodes.add("bgg224");
			
			updateapplyproductsBean.setProductCodes(productCodes);
			
			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();	
			
			Logger.verifyIsContains("批次不存在", updateapplyproductsCall.getMessage(), "验证返回Message");
//			Logger.verifyEquals("2", updateapplyproductsCall.getCode(), "验证返回StatusCode");

			List<YmtCouponProduct> products = new YmtCouponProductWapper().selectBysBatchId(batchId);
			
			Logger.verifyEquals(0, products.size(), "验证YmtCouponProduct表生成相应记录数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateApplyProducts_004() {
		Logger.start(true,"ProductCodes空");
		try {
			
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			CreateCouponBean createcouponBean = couponManageCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = couponManageCallService.createPlatformCoupon(createcouponBean);

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			int batchId =batch.getiBatchId();
			
			updateapplyproductsBean.setBatchId(batchId);
	
			List<String> productCodes = new ArrayList<>();
			
			updateapplyproductsBean.setProductCodes(productCodes);
			
			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();	
			
			Logger.verifyIsContains("productCodes:请传入商品编号", updateapplyproductsCall.getMessage(), "验证返回Message");
//			Logger.verifyEquals("2", updateapplyproductsCall.getCode(), "验证返回StatusCode");

			List<YmtCouponProduct> products = new YmtCouponProductWapper().selectBysBatchId(batchId);
			
			Logger.verifyEquals(0, products.size(), "验证YmtCouponProduct表生成相应记录数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateApplyProducts_005() {
		Logger.start(true,"更新优惠券适用商品-商家批次");
		try {
			CouponServiceMCallService couponCallServcie = new CouponServiceMCallService();

			SellerCreateBatchBean CouponBatchBean = couponCallServcie.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall = couponCallServcie.SellerCreateBatch(CouponBatchBean);

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			int batchId =batch.getiBatchId();
			
			updateapplyproductsBean.setBatchId(batchId);
	
			List<String> productCodes = new ArrayList<>();
			
			productCodes.add("Abc1233");
			productCodes.add("bgg224");
			
			updateapplyproductsBean.setProductCodes(productCodes);
			
			updateapplyproductsCall.setData(updateapplyproductsBean);
			updateapplyproductsCall.callService();	
			
			Logger.verifyIsContains("不是指定商品的批次", updateapplyproductsCall.getMessage(), "验证返回Message");
			Logger.verifyEquals("2", updateapplyproductsCall.getCode(), "验证返回StatusCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}